#pragma once

#include <windows.h>
#include "Win32Exception.hpp"
#include "Pipe.hpp"

namespace EGlib
{
  class Process;

  class ProcessRunner
  {
  public:
    static const int PIPE_BUFFERS_SIZE = 1024;

    int spawn_sync(Glib::RefPtr<Process> proc);

  private:
    void start_process();
    void handle_pipes();

    void set_up();
    void clean_up();

    Glib::RefPtr<Process>             proc;
    PROCESS_INFORMATION               proc_info;

    WritePipe*                        stdin_pipe;
    ReadPipe*                         stdout_pipe;
    ReadPipe*                         stderr_pipe;

    char  stdin_buffer[PIPE_BUFFERS_SIZE];
    char stdout_buffer[PIPE_BUFFERS_SIZE];
    char stderr_buffer[PIPE_BUFFERS_SIZE];

    void stdin_pipe_write_async();
    bool read_pipe_output_async(ReadPipe *pipe,char *buffer,
      Glib::RefPtr<Glib::IOChannel> channel);
    void write_to_channel(Glib::RefPtr<Glib::IOChannel> channel,
      char *buffer,gsize size);
  };
}
